788A - Functions again - CodeForces Solution


dp two pointers *1600

Please click on ads to support us..

Python Code:

import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")
def read_int(): return int(input())
def read_ints(): return map(int, input().split())
def read_ints_grid(n): return [get_ints() for _ in range(n)]
def read_str(): return input().strip()
def read_strs(): return get_str().split(' ')

MAX = int(1e10) + 5
mod = 10 ** 9 + 7



def solve():
    n = read_int()
    nums = list(read_ints())

    maxv = float('-inf')
    pre1 = pre2 = 0
    flag = 1
    for i in range(0, n-1):
        v1 = abs(nums[i+1] - nums[i]) * (flag)
        v2 = abs(nums[i+1] - nums[i]) * (-flag)
        pre1 = max(pre1 + v1, v1)
        pre2 = max(pre2 + v2, v2)
        maxv = max(maxv, pre1, pre2)
        flag = -flag
    print(maxv)

solve()

C++ Code:

    #include <bits/stdc++.h>
    
   using namespace std;
   
   #define int            long long int
   #define F              first
   #define S              second
   #define pb             push_back
   #define si             set <int>
   #define vi             vector <int>
   #define pii            pair <int, int>
   #define vpi            vector <pii>
   #define vpp            vector <pair<int, pii>>
   #define mii            map <int, int>
   #define mpi            map <pii, int>
   #define spi            set <pii>
   #define endl           "\n"
   #define sz(x)          ((int) x.size())
   #define all(p)         p.begin(), p.end()
   #define double         long double
   #define que_max        priority_queue <int>
   #define que_min        priority_queue <int, vi, greater<int>>
   #define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
   #define print(a)       for(auto x : a) cout << x << " "; cout << endl
   #define print1(a)      for(auto x : a) cout << x.F << " " << x.S << endl
   #define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
   
   inline int power(int a, int b)
   {
     int x = 1;
     while (b)
     {
       if (b & 1) x *= a;
       a *= a;
       b >>= 1;
     }
     return x;
   }
    int ceil_div(int a,int b)
    {
       return (a+b-1)/b;
    }
   
   template <typename Arg1>
   void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
   template <typename Arg1, typename... Args>
   void __f (const char* names, Arg1&& arg1, Args&&... args)
   {
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
   }
   vector<int> sieve(int n) {int*arr = new int[n + 1](); vector<int > vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
   const int N = 200005;
   
   void solve() {
      int n;
      cin>>n;
      int a[n];
      for(auto &it:a)cin>>it;
       vector<int>v;
       for(int i=1;i<n;i++){
       	v.pb(abs(a[i]-a[i-1]));
       }
       int ans=v[0];
       int sum=0;
       for(int i=0;i<n-1;i++){
       	if(i&1)sum+=v[i];
       	else sum-=v[i];
       	 ans=max(ans,sum);
       	 if(sum<0)sum=0;
       } 
       sum=0;
       for(int i=0;i<n-1;i++){
       	if(i&1)sum-=v[i];
       	else sum+=v[i];
       	 ans=max(ans,sum);
       	 if(sum<0)sum=0;
       }
       
   cout<<ans<<endl;
         
         
   }
   
   int32_t main()
   {
     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 
   
   
   
     clock_t z = clock();
   
     int t = 1;
       
     while (t--) solve();
   
     cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
   
     return 0;
   }


Comments

Submit
0 Comments
More Questions

870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class